package com.intel.two.service.medical.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.medical.DrugsMapper;
import com.intel.two.pojo.medical.Armarium;
import com.intel.two.pojo.medical.Drugs;
import com.intel.two.service.medical.DrugsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:xiaofeia
 * @DATE:2022/10/14
 */
@Service
public class DrugsServiceImpl implements DrugsService {

    @Autowired
    private DrugsMapper drugsMapper;



    @Override
    public LayuiPageVo<Drugs> drugsPageData(Map<String, Object> params) {
        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<Drugs> drugs =  drugsMapper.drugsPageData(params);

        // mybatis 的分页数据结构
        PageInfo<Drugs> armariumPageInfo = new PageInfo<>(drugs);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        LayuiPageVo<Drugs> layuiPageVo = new LayuiPageVo<>();
        layuiPageVo.setCode(0);
        layuiPageVo.setMsg("学生分页列表数据");
        layuiPageVo.setCount(armariumPageInfo.getTotal());  // 总记录数
        layuiPageVo.setData(armariumPageInfo.getList());  // 分页列表数据
        return layuiPageVo;
    }

    @Override
    public int addDrugs(Drugs drugs) {
        drugs.setDrugs_date(new Date());
        return drugsMapper.addDrugs(drugs);
    }

    @Override
    public int deleteDrugsById(Integer drugs_id) {
        return drugsMapper.deleteDrugsById(drugs_id);
    }

    @Override
    public int deletDrugsIds(String drugs_ids) {
        String[] _ids = drugs_ids.split(",");
        List<Integer> list = new ArrayList<>();
        for(int i=0;i < _ids.length; i++){
            list.add(Integer.parseInt(_ids[i]));
        }
        System.out.println(list);
        return drugsMapper.deletDrugsIds(list);
    }

    @Override
    public int updateDrugs(Drugs drugs) {
        return drugsMapper.updateDrugs(drugs);
    }
}
